package com.canin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.canin.model.Admin;

public class AdminDAO
{
	private Connection conn = null;

	public AdminDAO(Connection conn)
	{
		this.conn = conn;
	}

	public Admin getAdminByUserPass(String username, String password)
			throws SQLException
	{
		Admin admin = null;

		String sql = "SELECT * FROM ADMIN WHERE USERNAME = ? "
				+ "AND PASSWORD = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, username);
		ps.setString(2, password);

		ResultSet rs = ps.executeQuery();

		if (rs.next())
		{
			admin = new Admin();
			admin.setAdminId(rs.getInt("ID"));
			admin.setFirstName(rs.getString("FIRST_NAME"));
			admin.setLastName(rs.getString("LAST_NAME"));
			admin.setTelephone(rs.getString("TELEPHONE"));

			System.out.println("Admin : " + rs.getString("FIRST_NAME"));
		}

		rs.close();
		ps.close();

		return admin;
	}

	public Admin getAdminById(int id) throws Exception
	{
		Admin admin = null;

		String sql = "SELECT * FROM ADMIN WHERE ID = ? ";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, id);

		ResultSet rs = ps.executeQuery();

		if (rs.next())
		{
			admin = new Admin();

			admin.setAdminId(rs.getInt("ID"));
			admin.setFirstName(rs.getString("FIRST_NAME"));
			admin.setLastName(rs.getString("LAST_NAME"));
			admin.setTelephone(rs.getString("TELEPHONE"));
			System.out.println("Admin : " + rs.getString("FIRST_NAME"));
		}

		rs.close();
		ps.close();

		return admin;
	}
	
	public boolean createNewAdmin( Admin admin ) throws Exception
	{
		String sql = "insert into admin(first_name, last_name, telephone, username, password) " +
				"value( ?,?,?,?,? );";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, admin.getFirstName());
		ps.setString(2, admin.getLastName());
		ps.setString(3, admin.getTelephone());
		ps.setString(4, admin.getUsername());
		ps.setString(5, admin.getPassword());

		ps.executeUpdate();
		ps.close();
		
		return true;
	}
	
	public boolean updateAdmin( int id, String firstName, String lastName, String telephone ) throws Exception
	{
		String sql = "UPDATE ADMIN SET First_Name=?, Last_Name=?, Telephone=? WHERE id=?";
		
		System.out.println("\n\n" + sql + "\n\n");
		System.out.println( id + " : " + firstName + " : " + lastName + " : " + telephone );
		
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, firstName);
		ps.setString(2, lastName);
		ps.setString(3, telephone);
		ps.setInt(4, id);
		
		ps.executeUpdate();
		ps.close();
		
		return true;
	}

	public ArrayList<Admin> getAllAdmin() throws Exception
	{
		ArrayList<Admin> adminList = new ArrayList<Admin>();

		String sql = "select * from admin";
		PreparedStatement ps = conn.prepareStatement(sql);

		ResultSet rs = ps.executeQuery();

		while (rs.next())
		{
			Admin admin = new Admin();
			admin.setAdminId( rs.getInt( "ID" ) );
			admin.setFirstName( rs.getString( "first_name" ) );
			admin.setLastName( rs.getString( "last_name" ) );
			admin.setTelephone( rs.getString( "telephone" ) );
			admin.setUsername( rs.getString( "username" ) );
			
			adminList.add( admin );
		}

		rs.close();
		ps.close();

		return adminList;
	}
}
